/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using DaveSexton.DocProject.Resources;

namespace DaveSexton.DocProject
{
	public sealed class ToolBarCommandCollection : KeyedCollection<string, ToolBarCommand>
	{
		#region Private / Protected
		private readonly ToolBar toolBar;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ToolBarCommandCollection" /> class.
		/// </summary>
		internal ToolBarCommandCollection(ToolBar toolBar)
			: base(StringComparer.OrdinalIgnoreCase, -1)
		{
			if (toolBar == null)
				throw new ArgumentNullException("toolBar");

			this.toolBar = toolBar;
		}
		#endregion

		#region Methods
		protected override string GetKeyForItem(ToolBarCommand item)
		{
			return item.Name;
		}

		protected override void InsertItem(int index, ToolBarCommand item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			if (Items.Contains(item))
				throw new InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Errors.ToolBarCommandExists, item.Name));

			if (this.Contains(item.Name))
				throw new InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Errors.VSCommandNameExists, item.Name));

			base.InsertItem(index, item);

			item.ToolBar = toolBar;

			if (toolBar.CommandBar != null)
				DocProjectEnvironment.AddCommand(item);
		}

		protected override void SetItem(int index, ToolBarCommand item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			ToolBarCommand old = Items[index];

			if (old == item)
				return;

			if (Items.Contains(item))
				throw new InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Errors.ToolBarCommandExists, item.Name));

			if (this.Contains(item.Name))
				throw new InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Errors.VSCommandNameExists, item.Name));

			base.SetItem(index, item);

			old.ToolBar = null;

			if (toolBar.CommandBar != null)
				DocProjectEnvironment.RemoveCommand(old);

			item.ToolBar = toolBar;

			if (toolBar.CommandBar != null)
				DocProjectEnvironment.AddCommand(item);
		}

		protected override void RemoveItem(int index)
		{
			ToolBarCommand item = Items[index];

			item.ToolBar = null;

			if (toolBar.CommandBar != null)
				DocProjectEnvironment.RemoveCommand(item);

			base.RemoveItem(index);
		}

		protected override void ClearItems()
		{
			foreach (ToolBarCommand item in Items)
			{
				item.ToolBar = null;

				if (toolBar.CommandBar != null)
					DocProjectEnvironment.RemoveCommand(item);
			}

			base.ClearItems();
		}
		#endregion
	}
}
